package com.bawei.module_mine.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ItemGoldTransactionBinding
import com.bawei.module_mine.model.GoldTransaction
import com.bawei.module_mine.model.TransactionType

class GoldTransactionAdapter : RecyclerView.Adapter<GoldTransactionAdapter.TransactionViewHolder>() {

    private var transactions: List<GoldTransaction> = emptyList()

    fun updateTransactions(newTransactions: List<GoldTransaction>) {
        transactions = newTransactions
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder {
        val binding = ItemGoldTransactionBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return TransactionViewHolder(binding)
    }

    override fun getItemCount(): Int = transactions.size

    override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) {
        val transaction = transactions[position]
        holder.bind(transaction)
    }

    class TransactionViewHolder(private val binding: ItemGoldTransactionBinding) : 
        RecyclerView.ViewHolder(binding.root) {

        fun bind(transaction: GoldTransaction) {
            binding.apply {
                // 设置图标
                ivIcon.setImageResource(transaction.iconRes)
                ivIcon.setBackgroundResource(transaction.colorRes)
                
                // 设置描述
                tvDescription.text = transaction.description
                
                // 设置时间戳
                tvTimestamp.text = transaction.timestamp
                
                // 设置金额，根据交易类型设置不同的颜色和符号
                val amountText = when (transaction.type) {
                    TransactionType.EARN -> "+${transaction.amount}"
                    TransactionType.SPEND -> "-${transaction.amount}"
                    TransactionType.RECHARGE -> "+${transaction.amount}"
                    TransactionType.WITHDRAW -> "-${transaction.amount}"
                }
                tvAmount.text = amountText
                
                // 设置金额颜色
                val colorRes = when (transaction.type) {
                    TransactionType.EARN, TransactionType.RECHARGE -> R.color.success_green
                    TransactionType.SPEND, TransactionType.WITHDRAW -> R.color.error_red
                }
                tvAmount.setTextColor(itemView.context.getColor(colorRes))
            }
        }
    }
} 